package 工具函数模板;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class 快递分拣 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int s = scanner.nextInt();
        HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
        //由于hashmap是无序的 所以这里再创建一个记录城市输入顺序
        ArrayList<String> city = new ArrayList<>();
        for (int i = 0; i < s; i++) {
            String s1 = scanner.next();
            String s2 = scanner.next();
            if (!hashMap.containsKey(s2)) {
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(s1);
                city.add(s2);
                hashMap.put(s2, arrayList);
            } else {
                ArrayList<String> arrayList = hashMap.get(s2);
                arrayList.add(s1);
                hashMap.put(s2, arrayList);
            }
        }
        for (String c : city) {
            ArrayList<String> arrayList = hashMap.get(c);
            System.out.println(c+" "+arrayList.size());
            for (String num : arrayList) {
                System.out.println(num);
            }
        }

    }
}
